<?php
namespace Home\Controller;

use Home\Core\CommandSDK;

class OpenRecordController extends BaseController
{

    /**
     * 续费时长
     */
    protected $renew = [
        '0'=>'1',
        '1'=>'3',
        '2'=>'6',
        '3'=>'12',
        '4'=>'24',
        '5'=>'36',
    ];

    /**
     * 并发量
     */
    protected $count = [
        '0'=>'20',
        '1'=>'30',
        '2'=>'40',
        '3'=>'50',
    ];

    /**
     * 检索企业
     */
    public function open_service()
    {
        $param = I('get.');
        if(empty($param['pageid'])){
            $param['pageid']=1;
        }else{
            $param['pageid']=$param['pageid'];
        }
        if(empty($param['pagecount'])){
            $param['pagecount']=10;
        }else{
            $param['pagecount']=$param['pagecount'];
        }
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::openService, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $pageid=$param['pageid'];
        $pagecount=$param['pagecount'];
        $totnum=$return->totnum;
        $pagenum=$totnum/$pagecount;
        $this->assign('pageid', $pageid);//页数
        $this->assign('pagecount', $pagecount);//每页显示条数
        $this->assign('pagenum', $pagenum);//总页数
        $this->assign('totnum', $totnum); //总条数
        $this->assign('param',$param);   // 搜索条件
        $this->assign('array', $array);
        $this->display();
    }
    /**
     * 开通详情
     */
    public function service_details()
    {
        $param = I('get.');
        $param['begintime']=strtotime($param['begintime']);
        $param['endtime']=strtotime($param['endtime']);
        if(empty($param['pageid'])){
            $param['pageid']=1;
        }else{
            $param['pageid']=$param['pageid'];
        }
        if(empty($param['pagecount'])){
            $param['pagecount']=10;
        }else{
            $param['pagecount']=$param['pagecount'];
        }
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::resourcesDetails, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        foreach($array['details'] as $key=>&$value){
            $value['json'] = json_encode($value,JSON_UNESCAPED_UNICODE);   // 中文不转义
            if($value['serviceduration'] == 0  && $value['serviceduration'] !== NULL) {
                $array['details'][$key]['serviceduration'] = "7天";
            }elseif($value['serviceduration'] < 12 && $value['serviceduration'] >0){
                $a=$value['serviceduration'].个月;
                $array['details'][$key]['serviceduration']="$a";
            }elseif($value['serviceduration'] >= 12 ){
                $a=($value['serviceduration']%12).个月;
                $b=round($value['serviceduration']/12).年;
                if($a==0){
                    $array['details'][$key]['serviceduration']="$b";
                }else{
                    $array['details'][$key]['serviceduration']="$b".$a;
                }
            }
        }
        $freetest=$array['freetest'];
        $pageid=$param['pageid'];
        $pagecount=$param['pagecount'];
        $totnum=$return->totnum;
        $pagenum=$totnum/$pagecount;
        $companyid=$param['companyid'];
        $this->assign('companyid', $companyid);
        $this->assign('pageid', $pageid);//页数
        $this->assign('pagecount', $pagecount);//每页显示条数
        $this->assign('pagenum', $pagenum);//总页数
        $this->assign('totnum', $totnum); //总条数
        $this->assign('param',$param);   // 搜索条件
        $this->assign('freetest', $freetest);//开通机会 0 有 1 无
        $this->assign('array', $array);
        $this->display();
    }
    /**
     * 创建订单
     */
    public function service_order()
    {
        // 处理资源名称
        $companyid=$_GET['companyid'];
        $this->assign('companyid',$companyid);
        $resname = $_GET['resname'];
        $this->assign('resname',$resname);
        $resid = $_GET['resid'];
        $this->assign('resid',$resid);
        $resnum = count(explode(',',$resid))-1;  // 资源数量
        $this->assign('resnum',$resnum);
        $renew = [];
        foreach($this->renew as $key=>$value){
            $renew[$key]['endtime'] = date('Y-m-d',strtotime('+'.$value.' months',time()));
            $renew[$key]['renew'] = $value;
            $renew[$key]['renew_text'] = ($value < 12) ? $value.'个月' : floor($value/12).'年';
        }
        $this->assign('renew',$renew);   // 服务时长
        $this->assign('count',$this->count);   // 并发量
        $this->display();
    }
    /**
     * 提交订单
     */
    public function create_order_post()
    {
        $post = I('post.');
        $param['companyid'] = $post['companyid'];
        $param['resids'] = $post['resids'];
        $resnum = count(explode(',',$param['resids']))-1;
        $param['servicetype'] = (int)$post['servicetype'];
        $param['duration'] = (int)$post['duration'];
        $param['count'] = (int)$post['count'];
        $param['paymentmethod'] = (int)$post['paymentmethod'];
        $param['money'] = round($resnum*($param['duration']/12)*($param['count']/20)*350,2);
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::createOrder, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }
    /**
     * 续费
     */
    public function service_renew()
    {
        $info = json_decode($_GET['info'],true);
        if($info['serviceduration'] == 0){
            $info['servicecount']="7天";
        }elseif($info['serviceduration'] < 12 && $info['serviceduration'] !=0){
            $a=$info['serviceduration'].个月;
            $info['serviceduration']="$a";
        }elseif($info['serviceduration'] >= 12 ){
            $a=($info['serviceduration']%12).个月;
            $b=round($info['serviceduration']/12).年;
            if($a==0){
                $info['serviceduration']="$b";
            }else{
                $info['serviceduration']="$b".$a;
            }
        }
        $renew = [];
        // 不同续费时长的价格计算规则：资源数量(默认1)*（服务时长/12）*（并发量/20）*单价，单价=350/路/年（20并发）
        foreach($this->renew as $key=>$value){   // 计算不同的续费周期对应的金额、续费后的到期日期
            $renew[$key]['price'] = round(($value/12)*($info['servicecount']/20)*350,2);
            $renew[$key]['endtime'] = date("Y-m-d H:i:s", strtotime("+".$value." months", $info['endtime']));
            $renew[$key]['renew'] = $value;
            $renew[$key]['renew_text'] = ($value < 12) ? $value.'个月' : floor($value/12).'年';
        }
        $this->assign('renew',$renew);
        $this->assign('info',$info);
        $this->display();
    }
    /**
     * 提交续费
     */
    public function create_renew_post()
    {
        $post = I('post.');
        $param['resid'] = $post['resid'];
        $param['duration'] = $post['duration']['renew'];
        $param['paymentmethod'] = $post['paymentmethod'];
        $param['money'] = $post['duration']['price'];
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::renewal, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }
    /**
     * 升级服务
     */
    public function service_upgrade()
    {
        $info = json_decode($_GET['info'],true);
        if($info['serviceduration'] == 0){
            $info['servicecount']="7天";
        }elseif($info['serviceduration'] < 12 && $info['serviceduration'] !=0){
            $a=$info['serviceduration'].个月;
            $info['serviceduration']="$a";
        }elseif($info['serviceduration'] >= 12 ){
            $a=($info['serviceduration']%12).个月;
            $b=round($info['serviceduration']/12).年;
            if($a==0){
                $info['serviceduration']="$b";
            }else{
                $info['serviceduration']="$b".$a;
            }
        }
        $upgrade = [];
        // 不同并发量的价格计算规则：资源数量(默认1)*（到期时间-当前时间）*（新并发量-原并发量）*单价/365，单价=20元/路/年
        $days = ceil(($info['endtime']-time())/86400);   // 天数向上取整
        $key = 0;
        foreach($this->count as $value){   // 计算不同并发量对应的金额
            if($value < $info['servicecount']) {   // 并发量低于当前的过滤掉
                continue;
            }
            $upgrade[$key]['price'] = round($days*abs($value-$info['servicecount'])*20/365,2);
            $upgrade[$key]['upgrade'] = $value;
            $key ++;
        }
        $this->assign('upgrade',$upgrade);
        $this->assign('info',$info);
        $this->display();
    }
    /**
     * 提交升级服务
     */
    public function upgrade_post()
    {
        $post = I('post.');
        $param['resid'] = $post['resid'];
        $param['count'] = $post['count']['upgrade'];
        $param['paymentmethod'] = $post['paymentmethod'];
        $param['money'] = $post['count']['price'];
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::upgradeServices, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }
    /**
     * 免费试用
     */
    public function freeTrial_post()
    {
        $param=I('post.');
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::freeTrial, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }
    /**
     * 检索企业开通列表
     */
    public function open_record()
    {
        $param = I('get.');
        $param['begintime']=strtotime($param['begintime']);
        $param['endtime']=strtotime($param['endtime']);
        if(empty($param['pageid'])){
            $param['pageid']=1;
        }else{
            $param['pageid']=$param['pageid'];
        }
        if(empty($param['pagecount'])){
            $param['pagecount']=10;
        }else{
            $param['pagecount']=$param['pagecount'];
        }
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::openRecord, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $pageid=$param['pageid'];
        $pagecount=$param['pagecount'];
        $totnum=$return->totnum;
        $pagenum=$totnum/$pagecount;
        $this->assign('pageid', $pageid);//页数
        $this->assign('pagecount', $pagecount);//每页显示条数
        $this->assign('pagenum', $pagenum);//总页数
        $this->assign('totnum', $totnum); //总条数
        $this->assign('param',$param);   // 搜索条件
        $this->assign('array', $array);
        $this->display();
    }
    /**
     * 开通记录详情
     */
    public function record_details()
    {
        $param = I('get.');
        if(empty($param['pageid'])){
            $param['pageid']=1;
        }else{
            $param['pageid']=$param['pageid'];
        }
        if(empty($param['pagecount'])){
            $param['pagecount']=10;
        }else{
            $param['pagecount']=$param['pagecount'];
        }
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::openRecord, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        if($array['records'][0]['duration'] == 0){
            $array['records'][0]['duration']="7天";
        }elseif($array['records'][0]['duration'] < 12 && $array['records'][0]['duration'] !=0){
            $a=$array['records'][0]['duration'].个月;
            $array['records'][0]['duration']="$a";
        }elseif($array['records'][0]['duration'] >= 12 ){
            $a=($array['records'][0]['duration']%12).个月;
            $b=round($array['records'][0]['duration']/12).年;
            if($a==0){
                $array['records'][0]['duration']="$b";
            }else{
                $array['records'][0]['duration']="$b".$a;
            }
        }
        $this->assign('array', $array['records'][0]);
        $this->display();
    }

}